//
//  SkinUpdateInterface.m
//  CPICApp
//
//  Created by zhiwei li on 13-3-27.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "OperationLogView.h"
#import "Config.h"
#import "RCAlertViewUtil.h"
#import "NSString+SBJson.h"
#import "NSObject+SBJson.h"
#import "ViewController.h"
#import "AppDelegate.h"

@implementation OperationLogView
@synthesize progress;
@synthesize serialNo;
@synthesize code;
@synthesize type;

-(void)setOperationLogRequest//向太保移动平台发送操作日志接口请求报文
{
    NSDictionary *dictUserInfo=[[NSUserDefaults standardUserDefaults] dictionaryForKey:DICT_USER_INFO];
    NSString * UserAccout;
    if (dictUserInfo!=nil) 
    {
        if ([dictUserInfo valueForKey:@"UserAccount"])
        {
            NSLog(@"%@",[dictUserInfo valueForKey:@"UserAccount"]);
            UserAccout = [dictUserInfo valueForKey:@"UserAccount"];
        }
    }
    else 
    {
        UserAccout = @"";
    }
    
//    NSString *strRequestCode=@"RequestCode=60901";
    NSMutableDictionary *dicRequest = [NSMutableDictionary dictionary];
    [dicRequest setObject:UserAccout forKey:@"userAccount"];
    [dicRequest setObject:self.code forKey:@"code"];
    [dicRequest setObject:self.type forKey:@"type"];
    
    if ([self.type isEqualToString:@"1"]) 
    {
        [dicRequest setObject:@"" forKey:@"serialNo"];
    }
    else
    {
        if (self.serialNo.length==0) {
            return;
        }
        [dicRequest setObject:self.serialNo forKey:@"serialNo"];
    }
    NSString *strRequestJson=[dicRequest JSONRepresentation];
//    strRequestJson=[NSString stringWithFormat:@"&RequestData=%@",strRequestJson];
//    NSString *strUrl=[NSString stringWithFormat:@"%@%@%@",Server_Url,strRequestCode,strRequestJson];
//    NSString *strUrlEncod = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
//    NSURL *myurl = [NSURL URLWithString:strUrlEncod];//This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
//    NSLog(@"请求报文为:%@",strUrl);
     requestOperation= [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
    //设置需要POST的数据，这里提交两个数据，A=a&B=b
    [requestOperation setPostValue:@"60901" forKey:@"RequestCode"];
    [requestOperation setPostValue:strRequestJson forKey:@"RequestData"];
    
    [requestOperation setDelegate:self];
    [requestOperation setDidFailSelector:@selector(ResponseFail:)];
    [requestOperation setDidFinishSelector:@selector(ResonseSuccess:)];
    [requestOperation startAsynchronous];
    
    showProgress = YES;
    if (showProgress) 
    {
        progress= [[MBProgressHUD alloc]initWithView:self];
        [self addSubview:progress];
        [self bringSubviewToFront:progress];
        progress.labelText = @"加载中...";
        [progress show:NO];
        showProgress = NO;
        [progress setHidden:YES];
    };
}

- (void)ResonseSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
    
    NSMutableDictionary *jsonoSubObj = [dictResponse objectForKey:@"head"];
    NSString * ss = [[NSString alloc] initWithFormat:@"%@",[jsonoSubObj objectForKey:@"success"]];
    if ([ss isEqualToString:@"1"])
    {
        NSString * serialNostr = [dictResponse valueForKey:@"serialNo"];
        NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
        [accountDefaults setObject:serialNostr forKey:@"OPERATIONLOG"]; 
    }
    else
    {
        return;
    }
}

- (void)ResponseFail:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    [self justShowMessage:@"服务器连接失败" title:@"注意"];
}

-(id) initView:(NSString *)Code withtype:(NSString *)Type withserialno:(NSString *)Serialno;
{
    self.code = Code;
    self.type = Type;
    self.serialNo = Serialno;
    self = [super init];
    return self;
}

-(void) dealloc
{
    requestOperation.delegate=nil;
    [code release];
    [serialNo release];
    [type release];
    [progress release];
    [super dealloc];
}

@end
